from datetime import timedelta

from rest_framework import serializers
from user.models import User, Clazz


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['id','username','email','password','first_name','last_name','is_active','clazz','is_staff']
        extra_kwargs = {
            'password':{'write_only': True},
        }
    def create(self, validated_data):
        validated_data['is_active'] = True
        return User.objects.create_user(**validated_data)
class ClazzSerializer(serializers.ModelSerializer):
    graduate_time = serializers.SerializerMethodField(method_name='get_graduate_time')
    def get_graduate_time(self, model):
        return model.start_time + timedelta(days=model.duration)
    class Meta:
        model = Clazz
        fields = '__all__'